﻿$(function () {
    $("#optMedic").attr("class", "ListSelectedOption");
    $("#breadcrumb").append('<li><a href="' + $("#urlMedic").val() + '" title="Médicos">Médicos</a></li>');

    ///Toolbar
    $('#btnClear').live('click', function () {
        ClearForm();
    });

    $("#txtBornDate").datepicker({
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });

    $('#btnNew').live('click', function () {
        if (confirm('¿Desea abandonar la edición y crear un registro nuevo?')) {
            window.location = $('#urlCreate').val();
        }
    });

    ///Menu
    $('#menuClear').live('click', function () {
        ClearForm();
    });

    $('#menuNew').live('click', function () {
        if (confirm('¿Desea abandonar la edición y crear un registro nuevo?')) {
            window.location = $('#urlCreate').val();
        }
    });

    $('#imgPhoto').live('click', function () {
        $("#filePhoto").trigger("click");
    });


    var thumb = $('#imgPhoto');
    var thumb2 = $('#imgPhotoPreview');
    if (!(typeof thumb[0] === "undefined")) {
        new AjaxUpload('imgPhoto', {
            action: $("#urlUploadPhoto").val(),
            name: 'image',
            onSubmit: function (file, extension) { },
            onComplete: function (file, response) {
                $("#urlphoto").val(response);
                var url = '/ODSYSUI/Content/Images/Uploads/Medic/' + response;
                thumb.attr('src', url);
                thumb2.attr('src', url);
            }
        });
    }

});

function ConfirmDelete(id) {
    if (confirm('¿Esta seguro que desea eliminar el médico?')) {
        DeleteMedic(id);
    }
}

function DeleteMedic(objectId) {
    $.ajax(
    {
        type: "POST",
        url: $('#urlDelete').val(),
        data:
            JSON.stringify({
                id: objectId
            }),

        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            if (result.success) {
                window.location = "/ODSYSUI/Medic/SetMessage?id=1";
            }
            else {
                $("#lblErrorText").text(result.msg);
                $("#lblError").show(300).delay(5000).hide(400);
            }
        },
        error: function (req, status, error) {
            window.location = "/ODSYSUI/Medic/SetMessage?id=2";
        }
    });
}

function ClearForm() {
    //Limpia combos
    var cmbSpeciality = $('#cmbSpeciality');
    if (cmbSpeciality.length > 0) {
        cmbSpeciality[0].selectedIndex = 0;
    }

    //Limpia los textbox
    $('#txtID').val('');
    $('#txtName').val('');
    $('#txtFirstName').val('');
    $('#txtLastName').val('');
    $('#txtCodigo').val('');

    //Remueve todos los mensajes de error que genera el framework
    $('.field-validation-error').remove();
    $(":input").removeClass("input-validation-error");
}